﻿using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace Util
{
    class WinApi
    {
        [DllImport("../../Lib/KeyHook.dll")]
        public static extern void HookStart();

        [DllImport("../../Lib/KeyHook.dll")]
        public static extern void HookStop();

        [DllImport("user32.dll")]
        public static extern IntPtr SetFocus(IntPtr hWnd);

        [DllImport("user32.dll")]
        public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        [DllImport("user32.dll")]
        public static extern BoundsSpecified BringWindowToTop(IntPtr hWnd);

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetActiveWindow();

        [DllImport("user32", EntryPoint = "SetActiveWindow", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Ansi, SetLastError = true)]
        public static extern long SetActiveWindow(long hwnd);

        [DllImport("user32.dll")]
        public static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern int GetWindowTextLength(IntPtr hWnd);

          [DllImport("user32.dll", SetLastError = true)]
        public static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        // 현재 키의 눌려진 상태를 얻는다.
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern short GetKeyState(VirtualKeys Key_now);

        // 키보드 이벤트를 제어한다.
        [DllImport("user32.dll")]
        public static extern uint keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

        [DllImport("imm32.dll")]
        public static extern IntPtr ImmGetDefaultIMEWnd(IntPtr hWnd);

        [DllImport("imm32.dll")]
        public static extern IntPtr ImmReleaseContext(IntPtr hWnd, IntPtr hIMC);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, uint wParam, uint Iparam);

        public enum VirtualKeys : short
        {
            VK_0 = 0x30,
            VK_1 = 0x31,
            VK_2 = 50,
            VK_3 = 0x33,
            VK_4 = 0x34,
            VK_5 = 0x35,
            VK_6 = 0x36,
            VK_7 = 0x37,
            VK_8 = 0x38,
            VK_9 = 0x39,
            VK_A = 0x41,
            VK_ACCEPT = 30,
            VK_ADD = 0x6b,
            VK_APPS = 0x5d,
            VK_ATTN = 0xf6,
            VK_B = 0x42,
            VK_BACK = 8,
            VK_C = 0x43,
            VK_CANCEL = 3,
            VK_CAPITAL = 20,
            VK_CLEAR = 12,
            VK_CONTROL = 0x11,
            VK_CONVERT = 0x1c,
            VK_CRSEL = 0xf7,
            VK_D = 0x44,
            VK_DECIMAL = 110,
            VK_DELETE = 0x2e,
            VK_DIVIDE = 0x6f,
            VK_DOWN = 40,
            VK_E = 0x45,
            VK_END = 0x23,
            VK_EREOF = 0xf9,
            VK_ESCAPE = 0x1b,
            VK_EXECUTE = 0x2b,
            VK_EXSEL = 0xf8,
            VK_F = 70,
            VK_F1 = 0x70,
            VK_F10 = 0x79,
            VK_F11 = 0x7a,
            VK_F12 = 0x7b,
            VK_F2 = 0x71,
            VK_F3 = 0x72,
            VK_F4 = 0x73,
            VK_F5 = 0x74,
            VK_F6 = 0x75,
            VK_F7 = 0x76,
            VK_F8 = 0x77,
            VK_F9 = 120,
            VK_FINAL = 0x18,
            VK_G = 0x47,
            VK_H = 0x48,
            VK_HANGUL = 0x15,
            VK_HANJA = 0x19,
            VK_HELP = 0x2f,
            VK_HOME = 0x24,
            VK_I = 0x49,
            VK_INSERT = 0x2d,
            VK_J = 0x4a,
            VK_JUNJA = 0x17,
            VK_K = 0x4b,
            VK_L = 0x4c,
            VK_LBUTTON = 1,
            VK_LCONTROL = 0xa2,
            VK_LEFT = 0x25,
            VK_LMENU = 0xa4,
            VK_LSHIFT = 160,
            VK_LWIN = 0x5b,
            VK_M = 0x4d,
            VK_MBUTTON = 4,
            VK_MENU = 0x12,
            VK_MODECHANGE = 0x1f,
            VK_MULTIPLY = 0x6a,
            VK_N = 0x4e,
            VK_NEXT = 0x22,
            VK_NONAME = 0xfc,
            VK_NONCONVERT = 0x1d,
            VK_None = 0,
            VK_NUMLOCK = 0x90,
            VK_NUMPAD0 = 0x60,
            VK_NUMPAD1 = 0x61,
            VK_NUMPAD2 = 0x62,
            VK_NUMPAD3 = 0x63,
            VK_NUMPAD4 = 100,
            VK_NUMPAD5 = 0x65,
            VK_NUMPAD6 = 0x66,
            VK_NUMPAD7 = 0x67,
            VK_NUMPAD8 = 0x68,
            VK_NUMPAD9 = 0x69,
            VK_O = 0x4f,
            VK_OEM_1 = 0xba,
            VK_OEM_2 = 0xbf,
            VK_OEM_3 = 0xc0,
            VK_OEM_4 = 0xdb,
            VK_OEM_5 = 220,
            VK_OEM_6 = 0xdd,
            VK_OEM_7 = 0xde,
            VK_OEM_8 = 0xdf,
            VK_OEM_CLEAR = 0xfe,
            VK_OEM_COMMA = 0xbc,
            VK_OEM_MINUS = 0xbd,
            VK_OEM_PERIOD = 190,
            VK_OEM_PLUS = 0xbb,
            VK_P = 80,
            VK_PA1 = 0xfd,
            VK_PAUSE = 0x13,
            VK_PLAY = 250,
            VK_PRINT = 0x2a,
            VK_PRIOR = 0x21,
            VK_Q = 0x51,
            VK_R = 0x52,
            VK_RBUTTON = 2,
            VK_RCONTROL = 0xa3,
            VK_RETURN = 13,
            VK_RIGHT = 0x27,
            VK_RMENU = 0xa5,
            VK_RSHIFT = 0xa1,
            VK_RWIN = 0x5c,
            VK_S = 0x53,
            VK_SCROLL = 0x91,
            VK_SELECT = 0x29,
            VK_SEPARATOR = 0x6c,
            VK_SHIFT = 0x10,
            VK_SLEEP = 0x5f,
            VK_SNAPSHOT = 0x2c,
            VK_SPACE = 0x20,
            VK_SUBTRACT = 0x6d,
            VK_T = 0x54,
            VK_TAB = 9,
            VK_U = 0x55,
            VK_UP = 0x26,
            VK_V = 0x56,
            VK_W = 0x57,
            VK_X = 0x58,
            VK_XBUTTON1 = 5,
            VK_XBUTTON2 = 6,
            VK_Y = 0x59,
            VK_Z = 90,
            VK_ZOOM = 0xfb
        }
        // 마우스 이벤트 제어.
        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, UIntPtr dwExtraInfo);

    
        [DllImport("user32.dll")]
        public static extern int GetWindowLong(IntPtr hWnd, int nIndex);


        [DllImport("user32.dll")]
        public static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
        public enum GetWindow_Cmd : uint
        {
            GW_HWNDFIRST = 0,
            GW_HWNDLAST = 1,
            GW_HWNDNEXT = 2,
            GW_HWNDPREV = 3,
            GW_OWNER = 4,
            GW_CHILD = 5,
            GW_ENABLEDPOPUP = 6
        }
        [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
        public static extern IntPtr GetParent(IntPtr hWnd);



        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left;
            // x position of upper-left corner 
            public int Top;
            // y position of upper-left corner  
            public int Right;
            // x position of lower-right corner 
            public int Bottom;
            // y position of lower-right corner
        }
    }
}
